package com.imooc.common._this;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class FFmpegUtils {

    public static void convertVideoToAudio(String videoPath, String audioPath) {
        String ffmpegCommand = "ffmpeg -i " + videoPath + " -vn -ar 44100 -ac 2 -ab 192k " + audioPath;

        try {
            Process process = Runtime.getRuntime().exec(ffmpegCommand);
            BufferedReader reader = new BufferedReader(new InputStreamReader(process.getErrorStream()));
            String line;

            while ((line = reader.readLine()) != null) {
                // 在这里可以解析 line 来获取转换进度信息
                System.out.println(line);
            }

            int exitCode = process.waitFor();
            System.out.println("FFmpeg exited with code " + exitCode);
        } catch (IOException | InterruptedException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        // 示例用法
        convertVideoToAudio("/Users/dengwubo/Downloads/videoplayback2.mp4", "/Users/dengwubo/Downloads/videoplayback2.m4a");
    }
}
